<!DOCTYPE html>
<html>
  <head>
    <meta charset="utf-8" >

<title>利用coco api 获取mask并绘制 | KALUO</title>
<meta name="description" content="温故而知新">

<meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1, user-scalable=no">

<link rel="stylesheet" href="https://use.fontawesome.com/releases/v5.7.2/css/all.css" integrity="sha384-fnmOCqbTlWIlj8LyTjo7mOUStjsKC4pOpQbqyi7RrhN7udi9RwhKkMHpvLbHG9Sr" crossorigin="anonymous">
<link rel="shortcut icon" href="https://kaluo-zz.github.io/favicon.ico?v=1591084625855">
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/KaTeX/0.10.0/katex.min.css">
<link rel="stylesheet" href="https://kaluo-zz.github.io/styles/main.css">



<script src="https://cdn.jsdelivr.net/npm/vue/dist/vue.js"></script>
<script src="https://cdn.bootcss.com/highlight.js/9.12.0/highlight.min.js"></script>

<link rel="stylesheet" href="https://unpkg.com/aos@next/dist/aos.css" />



  </head>
  <body>
    <div id="app" class="main">

      <div class="sidebar" :class="{ 'full-height': menuVisible }">
  <div class="top-container" data-aos="fade-right">
    <div class="top-header-container">
      <a class="site-title-container" href="https://kaluo-zz.github.io">
        <img src="https://kaluo-zz.github.io/images/avatar.png?v=1591084625855" class="site-logo">
        <h1 class="site-title">KALUO</h1>
      </a>
      <div class="menu-btn" @click="menuVisible = !menuVisible">
        <div class="line"></div>
      </div>
    </div>
    <div>
      
        
          <a href="/" class="site-nav">
            首页
          </a>
        
      
        
          <a href="/archives" class="site-nav">
            归档
          </a>
        
      
        
          <a href="/tags" class="site-nav">
            标签
          </a>
        
      
        
          <a href="/post/about" class="site-nav">
            关于
          </a>
        
      
    </div>
  </div>
  <div class="bottom-container" data-aos="flip-up" data-aos-offset="0">
    <div class="social-container">
      
        
      
        
      
        
      
        
      
        
      
    </div>
    <div class="site-description">
      温故而知新
    </div>
    <div class="site-footer">
      Powered by <a href="https://github.com/getgridea/gridea" target="_blank">Gridea</a> | <a class="rss" href="https://kaluo-zz.github.io/atom.xml" target="_blank">RSS</a>
    </div>
  </div>
</div>


      <div class="main-container">
        <div class="content-container" data-aos="fade-up">
          <div class="post-detail">
            <h2 class="post-title">利用coco api 获取mask并绘制</h2>
            <div class="post-date">2019-10-30</div>
            
              <div class="feature-container" style="background-image: url('https://kaluo-zz.github.io/post-images/li-yong-coco-api-huo-qu-mask-bing-hui-zhi.jpg')">
              </div>
            
            <div class="post-content">
              <p>根据网络预测结果，构造coco格式的标注anns：</p>
<pre><code>{
    &quot;image_id&quot;: int,
    &quot;category_id&quot;: int,
    &quot;bbox&quot;: [x, y, w, h],
    &quot;score&quot;: float,
    &quot;segmentation&quot;: [[], [], ...]
}
</code></pre>
<p>举个例子，假如网络输出的格式为 pred = [xmin, ymin, xmax, ymax, scores, kps, clses]，则</p>
<pre><code class="language-python">color = [128, 0, 128]
if pred[4] &gt; thresh:
    image_id = batch['meta']['img_id'].cpu().numpy()[0]
    bbox = pred[:4].copy()
    bbox[2] -= bbox[0]
    bbox[3] -= bbox[1]
    bbox_out = list(map(lambda x: float(&quot;{:.2f}&quot;.format(x)), bbox))
    score = pred[4].copy()
    kps = pred[5:(5+2*num)].copy()
    kps = list(map(lambda x: float(&quot;{:.2f}&quot;.format(x)), kps)) # [x, y, x, y, ...]
    anns_pred = {
        &quot;image_id&quot;: int(image_id),
        &quot;category_id&quot;: 1, # 只是单个类别，person
        &quot;bbox&quot;: bbox_out,
        &quot;score&quot;: float(&quot;{:.2f}&quot;.format(score)),
        &quot;segmentation&quot;: [kps]
    }

    mask = coco.annToMask(anns)
    mask3 = np.ones((mask.shape[0], mask.shape[1], 3), dtype=np.float32)
    for j in range(3):
        mask3[:, :, j] = color[j]
        mask3[:, :, j] *= mask
    mask3 = mask3.astype(img.type)
    combined_img = cv2.addWeighted(mask3, 0.8, img, 1, 0)
</code></pre>
<p>上面示例只是显示了一个anns的img+mask结果。一张图像中的多个anns可以通过循环遍历来得到最终的mask:</p>
<pre><code>mask = np.zeros((h, w), dtype=np.float32)
for i in range(len(preds)):
    mask = cv2.bitwise_or(mask, coco.annToMask(preds[i]))
</code></pre>
<p>cv2.addWeighted()代码实例：<br>
https://www.programcreek.com/python/example/89436/cv2.addWeighted</p>

            </div>
            
              <div class="tag-container">
                
                  <a href="https://kaluo-zz.github.io/tag/6qPaooOHK" class="tag">
                    绘制mask
                  </a>
                
                  <a href="https://kaluo-zz.github.io/tag/vA0S58AABV" class="tag">
                    coco
                  </a>
                
                  <a href="https://kaluo-zz.github.io/tag/pSlACxuyg" class="tag">
                    python
                  </a>
                
              </div>
            
            
              <div class="next-post">
                <div class="next">下一篇</div>
                <a href="https://kaluo-zz.github.io/post/gpu-zhuang-tai-jian-ce">
                  <h3 class="post-title">
                    GPU状态监测
                  </h3>
                </a>
              </div>
            

            

          </div>

        </div>
      </div>
    </div>

    <script src="https://unpkg.com/aos@next/dist/aos.js"></script>

<script type="application/javascript">

AOS.init();

hljs.initHighlightingOnLoad()

var app = new Vue({
  el: '#app',
  data: {
    menuVisible: false,
  },
})

</script>




  </body>
</html>
